package org.example.sward.tooffer;

public class StrToInt {

    public int strToInt(String str) {
        char[] c = str.trim().toCharArray();
        if(c.length == 0){
            return 0;
        }
        int res = 0,bndry = Integer.MAX_VALUE / 10;
        int i = 1,sign = 1;
        if(c[0] == '-'){
            sign = -1;
        }else if(c[0] != '+'){
            i = 0;
        }
        for (int j = i; j < c.length; j++) {
            if(c[j] < '0'||c[j] > '9'){
                break;
            }
            if(res>bndry || res == bndry && c[j]>'7'){
                return sign == 1?Integer.MAX_VALUE:Integer.MIN_VALUE;
            }
            res = res * 10 + (c[j] - '0');
        }
        return sign * res;
    }

    public static void main(String[] args) {
        StrToInt s = new StrToInt();
//        int result = s.strToInt(" 4193 words and 987");
//        System.out.println(result);
//
//        int d = '6' - '1';
//        System.out.println(d);
        int[] priceArr = new int[]{1,2,3};
        int result = s.maxCakePrice(3,priceArr);
        System.out.println(result);
    }

    /**
     * 输入蛋糕的价格列表priceList,求重量为n蛋糕的最高售价
     * @param n
     * @param priceList
     * @return
     */
    public int maxCakePrice(int n, int[] priceList) {
        // 蛋糕重量 <= 1 时直接返回
        if (n <= 1){
            return priceList[n];
        }
        // 初始化 dp 列表
        int[] dp = new int[n + 1];
        // 按顺序计算 f(1), f(2), ..., f(n)
        for (int j = 1; j <= n; j++) {
            // 从 j 种组合种选择最高售价的组合作为 f(j)
            for (int i = 0; i < j; i++){
                dp[j] = Math.max(dp[j], dp[i] + priceList[j - i]);
            }
        }
        return dp[n];
    }

    public int maxPrice1(int n,int[] priceList,int[] dp){
        if(n<=1){
            return priceList[n];
        }
        int f_n = 0;
        for (int i = 0; i < n; i++) {
            int f_i = dp[i] != 0 ? dp[i] : maxPrice1(i,priceList,dp);
            f_n = Math.max(dp[i],f_i+priceList[n-i]);
        }
        dp[n] = f_n;
        return  f_n;
    }

}
